/**
 * @author Shine
 */

package com.shine.utils
{
	import flash.utils.ByteArray;
	
	public class StringUtil
	{
		public static function getPathExt(_path:String):String
		{
			var tempArr:Array = _path.split(".");
			var tempStr:String = tempArr[tempArr.length-1];
			return tempStr.toLowerCase();
		}
		
		public static function removeCDATALabels(targetString:String):String
		{
			if(isEmpty(targetString)) return "";
			return remove(remove(targetString,"<![CDATA["),"]]>");
		}
		
		/**
		 *	Replaces all instances of the replace string in the input string
		 *	with the replaceWith string.
		 *	@param input The string that instances of replace string will be 
		 *	replaces with removeWith string.
		 *	@param replace The string that will be replaced by instances of 
		 *	the replaceWith string.
		 *	@param replaceWith The string that will replace instances of replace
		 *	string.
		 *	@returns A new String with the replace string replaced with the 
		 *	replaceWith string.
		 */
		public static function replace(targetString:String , oldString:String , newString:String):String
		{
			return targetString.split(oldString).join(newString);
		}
		
		
		/**
		 * remove specified String and return this String
		 * @param target String
		 * @param remove String
		 * @return 
		 */	
		public static function remove(targetString:String, removeString:String):String
		{
			return replace(targetString, removeString, "");
		}
		
		/**
		 * HTML encode
		 */
		public static function htmlEncode(input:String):String
		{
			input = replace(input,"&","&amp;");
			input = replace(input,"\"","&quot;");
			input = replace(input,"'","&apos;");
			input = replace(input,"<","&lt;");
			input = replace(input,">","&gt;");
			return input;
		}
		
		/**
		 * validate Email address
		 */
		public static function validateEmail(input:String):Boolean
		{
			var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
			var result:Object = pattern.exec(input);
			if(result == null) {
				return false;
			}
			return true;
		}
		
		/**
		 * Encoding for String
		 * Change GB2312 to UTF8
		 */
		public static function GB2312toUTF8(input:*):String
		{
			var ba:ByteArray = ByteArray(input);
			return ba.readMultiByte(ba.length, "gb2312");
		}
		
		/**
		 *  Substitutes "{n}" tokens within the specified string
		 *  with the respective arguments passed in.
		 *
		 *  @param str The string to make substitutions in.
		 *  This string can contain special tokens of the form
		 *  <code>{n}</code>, where <code>n</code> is a zero based index,
		 *  that will be replaced with the additional parameters
		 *  found at that index if specified.
		 *
		 *  @param rest Additional parameters that can be substituted
		 *  in the <code>str</code> parameter at each <code>{n}</code>
		 *  location, where <code>n</code> is an integer (zero based)
		 *  index value into the array of values specified.
		 *  If the first parameter is an array this array will be used as
		 *  a parameter list.
		 *  This allows reuse of this routine in other methods that want to
		 *  use the ... rest signature.
		 *  For example <pre>
		 *     public function myTracer(str:String, ... rest):void
		 *     { 
		 *         label.text += StringUtil.substitute(str, rest) + "\n";
		 *     } </pre>
		 *
		 *  @return New string with all of the <code>{n}</code> tokens
		 *  replaced with the respective arguments specified.
		 *
		 *  @example
		 *
		 *  var str:String = "here is some info '{0}' and {1}";
		 *  Tracer.info(StringUtil.substitute(str, 15.4, true));
		 *
		 *  // this will output the following string:
		 *  // "here is some info '15.4' and true"
		 */
		public static function substitute(input:String, ... rest):String
		{
			// Replace all of the parameters in the msg string.
			var len:uint = rest.length;
			var args:Array;
			if (len == 1 && rest[0] is Array)
			{
				args = rest[0] as Array;
				len = args.length;
			} else {
				args = rest;
			}
			for (var i:int = 0; i < len; i++) {
				input = input.replace(new RegExp("\\{"+i+"\\}", "g"), args[i]);
			}
			return input;
		}
		
		/**
		 *	Removes whitespace from the front and the end of the specified
		 *	string.
		 *	@param input The String whose beginning and ending whitespace will
		 *	will be removed.
		 *	@returns A String with whitespace removed from the begining and end	
		 */			
		public static function trim(input:String):String
		{
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}
		
		/**
		 *	Removes whitespace from the front of the specified string.
		 *	@param input The String whose beginning whitespace will will be removed.
		 *	@returns A String with whitespace removed from the begining	
		 */	
		public static function ltrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = 0; i < size; i++)
			{
				if(input.charCodeAt(i) > 32)
				{
					return input.substring(i);
				}
			}
			return "";
		}
		
		/**
		 *	Removes whitespace from the end of the specified string.
		 *	@param input The String whose ending whitespace will will be removed.
		 *	@returns A String with whitespace removed from the end	
		 */	
		public static function rtrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = size; i > 0; i--)
			{
				if(input.charCodeAt(i - 1) > 32)
				{
					return input.substring(0, i);
				}
			}
			return "";
		}
		
		public static function isEmpty(input:String):Boolean
		{
			if ((((null == input)) || ((0 == input.length)))){
				return (true);
			};
			return ((trim(input).length == 0));
		}
		
		public static function escapeXML(input:String):String
		{
			var str:String=input;
			str= str.replace(/&/ig, "&amp;");
			
			while (str.indexOf("<") != -1) {
				str = str.replace("<", "&lt;");
			};
			
			while (str.indexOf(">") != -1) {
				str = str.replace(">", "&gt;");
			};
			
			return str;
		}
		
	}
}